# vim: syntax=ruby

# Parser using the Pops model, expression based

class Puppet::Pops::Parser::Parser

token STRING DQPRE DQMID DQPOST
token WORD
token LBRACK  RBRACK LBRACE RBRACE SYMBOL FARROW COMMA TRUE
token FALSE EQUALS APPENDS DELETES LESSEQUAL NOTEQUAL DOT COLON LLCOLLECT RRCOLLECT
token QMARK LPAREN RPAREN ISEQUAL GREATEREQUAL GREATERTHAN LESSTHAN
token IF ELSE
token DEFINE ELSIF VARIABLE CLASS INHERITS NODE BOOLEAN
token NAME SEMIC CASE DEFAULT AT ATAT LCOLLECT RCOLLECT CLASSREF
token NOT OR AND UNDEF PARROW PLUS MINUS TIMES DIV LSHIFT RSHIFT UMINUS
token MATCH NOMATCH REGEX IN_EDGE OUT_EDGE IN_EDGE_SUB OUT_EDGE_SUB
token IN UNLESS PIPE
token LAMBDA SELBRACE
token NUMBER
token HEREDOC SUBLOCATE
token RENDER_STRING RENDER_EXPR EPP_START EPP_END EPP_END_TRIM
token FUNCTION
token PRIVATE ATTR TYPE
token FUNCTION
token LOW

prechigh
  left  HIGH
  left  SEMIC
  left  PIPE
  left  LPAREN
  left  RPAREN
  left  DOT
  nonassoc EPP_START
  left  LBRACK LISTSTART
  left  RBRACK
  left  QMARK
  left  LCOLLECT LLCOLLECT
  right NOT
  nonassoc SPLAT
  nonassoc UMINUS
  left  IN
  left  MATCH NOMATCH
  left  TIMES DIV MODULO
  left  MINUS PLUS
  left  LSHIFT RSHIFT
  left  NOTEQUAL ISEQUAL
  left  GREATEREQUAL GREATERTHAN LESSTHAN LESSEQUAL
  left  AND
  left  OR
  left  LBRACE
  left  SELBRACE
  left  RBRACE
  right AT ATAT
  right APPENDS DELETES EQUALS
  left  IN_EDGE OUT_EDGE IN_EDGE_SUB OUT_EDGE_SUB
  left  FARROW
  left  COMMA
  nonassoc RENDER_EXPR
  nonassoc RENDER_STRING
  left  LOW
preclow

rule
# Produces [Model::Program] with a body containing what was parsed
program
  : statements     { result = create_program(Factory.block_or_expression(*val[0])) }
  | epp_expression { result = create_program(Factory.block_or_expression(*val[0])) }
  |                { result = create_empty_program() }

# Produces a semantic model (non validated, but semantically adjusted).
statements
  : syntactic_statements { result = transform_calls(val[0]) }

# Collects sequence of elements into a list that the statements rule can transform
# (Needed because language supports function calls without parentheses around arguments).
# Produces Array<Model::Expression>
#
syntactic_statements
  : syntactic_statement                            { result = [val[0]]}
  | syntactic_statements SEMIC syntactic_statement { result = val[0].push val[2] }
  | syntactic_statements syntactic_statement       { result = val[0].push val[1] }

# Produce a single expression or Array of expression
# This exists to handle multiple arguments to non parenthesized function call. If e is expression,
# the a program can consists of e [e,e,e] where the first may be a name of a function to call.
#
syntactic_statement
  : assignment                             =LOW { result = val[0] }
  | syntactic_statement COMMA assignment   =LOW { result = aryfy(val[0]).push(val[1]).push(val[2]) }

# Assignment (is right recursive since assignment is right associative)
assignment
  : relationship =LOW
  | relationship EQUALS  assignment         { result = val[0].set(val[2])      ; loc result, val[1] }
  | relationship APPENDS assignment         { result = val[0].plus_set(val[2]) ; loc result, val[1] }
  | relationship DELETES assignment         { result = val[0].minus_set(val[2]); loc result, val[1] }

assignments
  : assignment                              { result = [val[0]] }
  | assignments COMMA assignment            { result = val[0].push(val[2]) }

relationship
  : resource =LOW
  | relationship IN_EDGE      resource      { result = val[0].relop(val[1][:value], val[2]); loc result, val[1] }
  | relationship IN_EDGE_SUB  resource      { result = val[0].relop(val[1][:value], val[2]); loc result, val[1] }
  | relationship OUT_EDGE     resource      { result = val[0].relop(val[1][:value], val[2]); loc result, val[1] }
  | relationship OUT_EDGE_SUB resource      { result = val[0].relop(val[1][:value], val[2]); loc result, val[1] }

#-- RESOURCE
#
resource
  : expression = LOW

    #---VIRTUAL
  | AT resource {
      result = val[1]
      unless Factory.set_resource_form(result, :virtual)
        # This is equivalent to a syntax error - additional semantic restrictions apply
        error val[0], "Virtual (@) can only be applied to a Resource Expression"
      end
      # relocate the result
      loc result, val[0], val[1]
    }

    #---EXPORTED
  | ATAT resource {
      result = val[1]
      unless Factory.set_resource_form(result, :exported)
        # This is equivalent to a syntax error - additional semantic restrictions apply
        error val[0], "Exported (@@) can only be applied to a Resource Expression"
      end
      # relocate the result
      loc result, val[0], val[1]
    }

    #---RESOURCE TITLED 3x and 4x
  | resource LBRACE expression COLON attribute_operations additional_resource_bodies RBRACE {
      bodies = [Factory.RESOURCE_BODY(val[2], val[4])] + val[5]
      result = Factory.RESOURCE(val[0], bodies)
      loc result, val[0], val[6]
    }

    #---CLASS RESOURCE
  | CLASS LBRACE resource_bodies endsemi RBRACE {
        result = Factory.RESOURCE(Factory.fqn(token_text(val[0])), val[2])
        loc result, val[0], val[4]
    }

    # --RESOURCE 3X Expression
    #   Handles both 3x overrides and defaults (i.e. single resource_body without title colon)
    #   Slated for possible deprecation since it requires transformation and mix static/evaluation check
    #
  | resource LBRACE attribute_operations endcomma RBRACE {
       result = case Factory.resource_shape(val[0])
       when :resource, :class
        # This catches deprecated syntax.
        # If the attribute operations does not include +>, then the found expression
        # is actually a LEFT followed by LITERAL_HASH
        #
        unless tmp = transform_resource_wo_title(val[0], val[2])
          error val[1], "Syntax error resource body without title or hash with +>"
        end
        tmp
      when :defaults
        Factory.RESOURCE_DEFAULTS(val[0], val[2])
      when :override
        # This was only done for override in original - TODO should it be here at all
        Factory.RESOURCE_OVERRIDE(val[0], val[2])
      else
        error val[0], "Expression is not valid as a resource, resource-default, or resource-override"
      end
     loc result, val[0], val[4]
    }

  resource_body
    : expression COLON attribute_operations endcomma { result = Factory.RESOURCE_BODY(val[0], val[2]) }

  resource_bodies
    : resource_body                         =HIGH { result = [val[0]] }
    | resource_bodies SEMIC resource_body   =HIGH { result = val[0].push val[2] }

  # This is a rule for the intermediate state where RACC has seen enough tokens to understand that
  # what is expressed is a Resource Expression, it now has to get to the finishing line
  #
  additional_resource_bodies
    : endcomma                         { result = [] }
    | endcomma SEMIC                   { result = [] }
    | endcomma SEMIC resource_bodies endsemi { result = val[2] }

#-- EXPRESSION
#
expression
  : primary_expression
  | call_function_expression
  | expression LBRACK       expressions RBRACK =LBRACK { result = val[0][*val[2]]         ; loc result, val[0], val[3] }
  | expression IN           expression                 { result = val[0].in val[2]        ; loc result, val[1] }
  | expression MATCH        expression                 { result = val[0] =~  val[2]       ; loc result, val[1] }
  | expression NOMATCH      expression                 { result = val[0].mne val[2]       ; loc result, val[1] }
  | expression PLUS         expression                 { result = val[0] +   val[2]       ; loc result, val[1] }
  | expression MINUS        expression                 { result = val[0] -   val[2]       ; loc result, val[1] }
  | expression DIV          expression                 { result = val[0] /   val[2]       ; loc result, val[1] }
  | expression TIMES        expression                 { result = val[0] *   val[2]       ; loc result, val[1] }
  | expression MODULO       expression                 { result = val[0] %   val[2]       ; loc result, val[1] }
  | expression LSHIFT       expression                 { result = val[0] <<  val[2]       ; loc result, val[1] }
  | expression RSHIFT       expression                 { result = val[0] >>  val[2]       ; loc result, val[1] }
  |            MINUS        expression         =UMINUS { result = val[1].minus()          ; loc result, val[0] }
  |            TIMES        expression         =SPLAT  { result = val[1].unfold()         ; loc result, val[0] }
  | expression NOTEQUAL     expression                 { result = val[0].ne  val[2]       ; loc result, val[1] }
  | expression ISEQUAL      expression                 { result = val[0] ==  val[2]       ; loc result, val[1] }
  | expression GREATERTHAN  expression                 { result = val[0] >   val[2]       ; loc result, val[1] }
  | expression GREATEREQUAL expression                 { result = val[0] >=  val[2]       ; loc result, val[1] }
  | expression LESSTHAN     expression                 { result = val[0] <   val[2]       ; loc result, val[1] }
  | expression LESSEQUAL    expression                 { result = val[0] <=  val[2]       ; loc result, val[1] }
  |            NOT          expression                 { result = val[1].not              ; loc result, val[0] }
  | expression AND          expression                 { result = val[0].and val[2]       ; loc result, val[1] }
  | expression OR           expression                 { result = val[0].or  val[2]       ; loc result, val[1] }
  | expression QMARK selector_entries                  { result = val[0].select(*val[2])  ; loc result, val[0] }
  |            LPAREN       assignment RPAREN          { result = val[1].paren()          ; loc result, val[0] }


#---EXPRESSIONS
#   (i.e. "argument list")
#
# This expression list can not contain function calls without parentheses around arguments
# Produces Array<Model::Expression>
#
expressions
  : expression                   { result = [val[0]] }
  | expressions COMMA expression { result = val[0].push(val[2]) }

primary_expression
  : variable
  | call_method_with_lambda_expression
  | collection_expression
  | case_expression
  | if_expression
  | unless_expression
  | definition_expression
  | hostclass_expression
  | node_definition_expression
  | epp_render_expression
  | function_definition
  | reserved_word
  | array
  | hash
  | regex
  | quotedtext
  | type
  | NUMBER       { result = Factory.NUMBER(val[0][:value])          ; loc result, val[0] }
  | BOOLEAN      { result = Factory.literal(val[0][:value])         ; loc result, val[0] }
  | DEFAULT      { result = Factory.literal(:default)               ; loc result, val[0] }
  | UNDEF        { result = Factory.literal(:undef)                 ; loc result, val[0] }
  | NAME         { result = Factory.QNAME_OR_NUMBER(val[0][:value]) ; loc result, val[0] }


#---CALL FUNCTION
#
# Produces Model::CallNamedFunction

call_function_expression
  : expression LPAREN assignments endcomma RPAREN {
      result = Factory.CALL_NAMED(val[0], true, val[2])
      loc result, val[0], val[4]
    }
  | expression LPAREN RPAREN {
      result = Factory.CALL_NAMED(val[0], true, [])
      loc result, val[0], val[2]
    }
  | expression LPAREN assignments endcomma RPAREN lambda {
      result = Factory.CALL_NAMED(val[0], true, val[2])
      loc result, val[0], val[4]
      result.lambda = val[5]
    }
  | expression LPAREN RPAREN  lambda {
      result = Factory.CALL_NAMED(val[0], true, [])
      loc result, val[0], val[2]
      result.lambda = val[3]
    }

#---CALL METHOD
#
call_method_with_lambda_expression
  : call_method_expression         =LOW { result = val[0] }
  | call_method_expression lambda       { result = val[0]; val[0].lambda = val[1] }

  call_method_expression
    : named_access LPAREN assignments RPAREN { result = Factory.CALL_METHOD(val[0], val[2]); loc result, val[1], val[3] }
    | named_access LPAREN RPAREN             { result = Factory.CALL_METHOD(val[0], []); loc result, val[1], val[3] }
    | named_access =LOW                      { result = Factory.CALL_METHOD(val[0], []); loc result, val[0] }

  named_access
    : expression DOT NAME {
        result = val[0].dot(Factory.fqn(val[2][:value]))
        loc result, val[1], val[2]
      }

#---LAMBDA
#
lambda
  : lambda_parameter_list lambda_rest {
      result = Factory.LAMBDA(val[0][:value], val[1][:value])
      loc result, val[0][:start], val[1][:end]
    }

lambda_rest
  : LBRACE statements RBRACE  { result = {:end => val[2], :value =>val[1] } }
  | LBRACE RBRACE             { result = {:end => val[1], :value => nil } }


lambda_parameter_list
  : PIPE PIPE                     { result = {:start => val[0], :value => [] } }
  | PIPE parameters endcomma PIPE { result = {:start => val[0], :value => val[1] } }

#---CONDITIONALS

#--IF
#
if_expression
  : IF if_part {
      result = val[1]
      loc(result, val[0], val[1])
    }

  # Produces Model::IfExpression
  if_part
    : expression LBRACE statements RBRACE else {
        result = Factory.IF(val[0], Factory.block_or_expression(*val[2]), val[4])
        loc(result, val[0], (val[4] ? val[4] : val[3]))
      }
    | expression LBRACE RBRACE else {
        result = Factory.IF(val[0], nil, val[3])
        loc(result, val[0], (val[3] ? val[3] : val[2]))
      }

  # Produces [Model::Expression, nil] - nil if there is no else or elsif part
  else
    : # nothing
    | ELSIF if_part {
        result = val[1]
        loc(result, val[0], val[1])
      }
    | ELSE LBRACE statements RBRACE {
        result = Factory.block_or_expression(*val[2])
        loc result, val[0], val[3]
      }
    | ELSE LBRACE RBRACE {
        result = nil # don't think a nop is needed here either
      }

#--UNLESS
#
unless_expression
  : UNLESS expression LBRACE statements RBRACE unless_else {
      result = Factory.UNLESS(val[1], Factory.block_or_expression(*val[3]), val[5])
      loc result, val[0], val[4]
    }
  | UNLESS expression LBRACE RBRACE unless_else {
      result = Factory.UNLESS(val[1], nil, val[4])
      loc result, val[0], val[4]
    }

  # Different from else part of if, since "elsif" is not supported, but 'else' is
  #
  # Produces [Model::Expression, nil] - nil if there is no else or elsif part
  unless_else
    : # nothing
    | ELSE LBRACE statements RBRACE {
        result = Factory.block_or_expression(*val[2])
        loc result, val[0], val[3]
      }
    | ELSE LBRACE RBRACE {
        result = nil # don't think a nop is needed here either
      }

#--- CASE EXPRESSION
#
case_expression
  : CASE expression LBRACE case_options RBRACE {
      result = Factory.CASE(val[1], *val[3])
      loc result, val[0], val[4]
    }

  # Produces Array<Model::CaseOption>
  case_options
    : case_option               { result = [val[0]] }
    | case_options case_option  { result = val[0].push val[1] }

  # Produced Model::CaseOption (aka When)
  case_option
    : expressions COLON LBRACE options_statements RBRACE {
        result = Factory.WHEN(val[0], val[3]); loc result, val[1], val[4]
      }

  options_statements
    : nil
    | statements

  # This special construct is required or racc will produce the wrong result when the selector entry
  # LHS is generalized to any expression (LBRACE looks like a hash). Thus it is not possible to write
  # a selector with a single entry where the entry LHS is a hash.
  # The SELBRACE token is a LBRACE that follows a QMARK, and this is produced by the lexer with a lookback
  # Produces Array<Model::SelectorEntry>
  #
  selector_entries
    : selector_entry
    | SELBRACE selector_entry_list endcomma RBRACE {
        result = val[1]
      }

  # Produces Array<Model::SelectorEntry>
  selector_entry_list
    : selector_entry                           { result = [val[0]] }
    | selector_entry_list COMMA selector_entry { result = val[0].push val[2] }

  # Produces a Model::SelectorEntry
  # This FARROW wins over FARROW in Hash
  selector_entry
    : expression FARROW expression { result = Factory.MAP(val[0], val[2]) ; loc result, val[1] }

#---COLLECTION
#
# A Collection is a predicate applied to a set of objects with an implied context (used variables are
# attributes of the object.
# i.e. this is equivalent to source.select(QUERY).apply(ATTRIBUTE_OPERATIONS)
#
collection_expression
  : expression collect_query LBRACE attribute_operations endcomma RBRACE {
      result = Factory.COLLECT(val[0], val[1], val[3])
      loc result, val[0], val[5]
    }
  | expression collect_query =LOW {
      result = Factory.COLLECT(val[0], val[1], [])
      loc result, val[0], val[1]
    }

  collect_query
    : LCOLLECT  optional_query RCOLLECT  { result = Factory.VIRTUAL_QUERY(val[1])   ; loc result, val[0], val[2] }
    | LLCOLLECT optional_query RRCOLLECT { result = Factory.EXPORTED_QUERY(val[1])  ; loc result, val[0], val[2] }

  optional_query
    : nil
    | expression

#---ATTRIBUTE OPERATIONS (Not an expression)
#
attribute_operations
  :                                                { result = [] }
  | attribute_operation                            { result = [val[0]] }
  | attribute_operations COMMA attribute_operation { result = val[0].push(val[2]) }

  # Produces String
  # QUESTION: Why is BOOLEAN valid as an attribute name?
  #
  attribute_name
    : NAME
    | keyword
#    | BOOLEAN

  # In this version, illegal combinations are validated instead of producing syntax errors
  # (Can give nicer error message "+> is not applicable to...")
  # Produces Model::AttributeOperation
  #
  attribute_operation
    : attribute_name FARROW expression {
        result = Factory.ATTRIBUTE_OP(val[0][:value], :'=>', val[2])
        loc result, val[0], val[2]
      }
    | attribute_name PARROW expression {
        result = Factory.ATTRIBUTE_OP(val[0][:value], :'+>', val[2])
        loc result, val[0], val[2]
      }
  | TIMES FARROW expression {
      result = Factory.ATTRIBUTES_OP(val[2]) ; loc result, val[0], val[2]
   }

#---DEFINE
#
# Produces Model::Definition
#
definition_expression
  : DEFINE classname parameter_list LBRACE opt_statements RBRACE {
      result = add_definition(Factory.DEFINITION(classname(val[1][:value]), val[2], val[4]))
      loc result, val[0], val[5]
      # New lexer does not keep track of this, this is done in validation
      if @lexer.respond_to?(:'indefine=')
        @lexer.indefine = false
      end
    }

#---HOSTCLASS
#
# Produces Model::HostClassDefinition
#
hostclass_expression
  : CLASS stacked_classname parameter_list classparent LBRACE opt_statements RBRACE {
      # Remove this class' name from the namestack as all nested classes have been parsed
      namepop
      result = add_definition(Factory.HOSTCLASS(classname(val[1][:value]), val[2], token_text(val[3]), val[5]))
      loc result, val[0], val[6]
    }

  # Record the classname so nested classes gets a fully qualified name at parse-time
  # This is a separate rule since racc does not support intermediate actions.
  #
  stacked_classname
    : classname { namestack(val[0][:value]) ; result = val[0] }

  opt_statements
    : statements
    | nil

  # Produces String, name or nil result
  classparent
    : nil
    | INHERITS classnameordefault { result = val[1] }

  # Produces String (this construct allows a class to be named "default" and to be referenced as
  # the parent class.
  # TODO: Investigate the validity
  # Produces a String (classname), or a token (DEFAULT).
  #
  classnameordefault
    : classname
    | DEFAULT

#---NODE
#
# Produces Model::NodeDefinition
#
node_definition_expression
  : NODE hostnames endcomma nodeparent LBRACE statements RBRACE {
      result = add_definition(Factory.NODE(val[1], val[3], val[5]))
      loc result, val[0], val[6]
    }
  | NODE hostnames endcomma nodeparent LBRACE RBRACE {
      result = add_definition(Factory.NODE(val[1], val[3], nil))
      loc result, val[0], val[5]
    }

  # Hostnames is not a list of names, it is a list of name matchers (including a Regexp).
  # (The old implementation had a special "Hostname" object with some minimal validation)
  #
  # Produces Array<Model::LiteralExpression>
  #
  hostnames
    : hostname                 { result = [result] }
    | hostnames COMMA hostname { result = val[0].push(val[2]) }

  # Produces a LiteralExpression (string, :default, or regexp)
  # String with interpolation is validated for better error message
  hostname
    : dotted_name
    | quotedtext
    | DEFAULT     { result = Factory.literal(:default); loc result, val[0] }
    | regex

  dotted_name
    : name_or_number   { result = Factory.literal(val[0][:value]); loc result, val[0] }
    | dotted_name DOT name_or_number { result = Factory.concat(val[0], '.', val[2][:value]); loc result, val[0], val[2] }

  name_or_number
    : NAME
    | NUMBER

  # Produces Expression, since hostname is an Expression
  nodeparent
    : nil
    | INHERITS hostname { result = val[1] }

#---FUNCTION DEFINITION
#
function_definition
  : FUNCTION classname parameter_list LBRACE opt_statements RBRACE {
      result = add_definition(Factory.FUNCTION(val[1][:value], val[2], val[4]))
      loc result, val[0], val[5]
    }

#---NAMES AND PARAMETERS COMMON TO SEVERAL RULES
#   Produces String
#   TODO: The error that "class" is not a valid classname is bad - classname rule is also used for other things
classname
  : NAME
  | WORD
  | CLASSREF
  | CLASS { error val[0], "'class' is not a valid classname" }

# Produces Array<Model::Parameter>
parameter_list
  : nil                               { result = [] }
  | LPAREN  RPAREN                    { result = [] }
  | LPAREN parameters endcomma RPAREN { result = val[1] }

# Produces Array<Model::Parameter>
parameters
  : parameter                  { result = [val[0]] }
  | parameters COMMA parameter { result = val[0].push(val[2]) }

# Produces Model::Parameter
parameter
  : untyped_parameter
  | typed_parameter

untyped_parameter
  : regular_parameter
  | splat_parameter

regular_parameter
  : VARIABLE EQUALS expression { result = Factory.PARAM(val[0][:value], val[2]) ; loc result, val[0] }
  | VARIABLE                   { result = Factory.PARAM(val[0][:value]); loc result, val[0] }

splat_parameter
  : TIMES regular_parameter { result = val[1]; val[1].captures_rest() }

typed_parameter
  : parameter_type untyped_parameter { val[1].type_expr(val[0]) ; result = val[1] }

parameter_type
  : type { result = val[0] }
  | type LBRACK expressions RBRACK { result = val[0][*val[2]] ; loc result, val[0], val[3] }

#--VARIABLE
#
variable
  : VARIABLE { result = Factory.fqn(val[0][:value]).var ; loc result, val[0] }

#---RESERVED WORDS
#
reserved_word
  : PRIVATE   { result = Factory.RESERVED(val[0][:value]) ; loc result, val[0] }
  | TYPE      { result = Factory.RESERVED(val[0][:value]) ; loc result, val[0] }
  | ATTR      { result = Factory.RESERVED(val[0][:value]) ; loc result, val[0] }

#---LITERALS (dynamic and static)
#

array
  : LISTSTART assignments endcomma  RBRACK { result = Factory.LIST(val[1]); loc result, val[0], val[3] }
  | LISTSTART                       RBRACK { result = Factory.literal([]) ; loc result, val[0], val[1] }
  | LBRACK    assignments endcomma  RBRACK { result = Factory.LIST(val[1]); loc result, val[0], val[3] }
  | LBRACK                          RBRACK { result = Factory.literal([]) ; loc result, val[0], val[1] }

hash
  : LBRACE hashpairs RBRACE       { result = Factory.HASH(val[1]); loc result, val[0], val[2] }
  | LBRACE hashpairs COMMA RBRACE { result = Factory.HASH(val[1]); loc result, val[0], val[3] }
  | LBRACE RBRACE                 { result = Factory.literal({}) ; loc result, val[0], val[1] }

  hashpairs
    : hashpair                 { result = [val[0]] }
    | hashpairs COMMA hashpair { result = val[0].push val[2] }

  hashpair
    : assignment FARROW assignment { result = Factory.KEY_ENTRY(val[0], val[2]); loc result, val[1] }

quotedtext
  : string
  | dq_string
  | heredoc

string
  : STRING  { result = Factory.literal(val[0][:value]) ; loc result, val[0] }
  | WORD    { result = Factory.literal(val[0][:value]) ; loc result, val[0] }

dq_string       : dqpre dqrval           { result = Factory.string(val[0], *val[1]) ; loc result, val[0], val[1][-1] }
dqpre           : DQPRE                  { result = Factory.literal(val[0][:value]); loc result, val[0] }
dqpost          : DQPOST                 { result = Factory.literal(val[0][:value]); loc result, val[0] }
dqmid           : DQMID                  { result = Factory.literal(val[0][:value]); loc result, val[0] }
dqrval          : text_expression dqtail { result = [val[0]] + val[1] }
text_expression : assignment             { result = Factory.TEXT(val[0]) }

dqtail
  : dqpost        { result = [val[0]] }
  | dqmid dqrval  { result = [val[0]] + val[1] }

heredoc
  : HEREDOC sublocated_text  { result = Factory.HEREDOC(val[0][:value], val[1]); loc result, val[0] }

sublocated_text
  : SUBLOCATE string    { result = Factory.SUBLOCATE(val[0], val[1]); loc result, val[0] }
  | SUBLOCATE dq_string { result = Factory.SUBLOCATE(val[0], val[1]); loc result, val[0] }

epp_expression
  : EPP_START epp_parameters_list optional_statements { result = Factory.EPP(val[1], val[2]); loc result, val[0] }

optional_statements
  :
  | statements

epp_parameters_list
  :                                  =LOW{ result = nil }
  | PIPE PIPE                        { result = [] }
  | PIPE parameters endcomma PIPE    { result = val[1] }

epp_render_expression
  : RENDER_STRING                  { result = Factory.RENDER_STRING(val[0][:value]); loc result, val[0] }
  | RENDER_EXPR expression epp_end { result = Factory.RENDER_EXPR(val[1]); loc result, val[0], val[2] }
  | RENDER_EXPR LBRACE statements RBRACE epp_end { result = Factory.RENDER_EXPR(Factory.block_or_expression(*val[2])); loc result, val[0], val[4] }

epp_end
  : EPP_END
  | EPP_END_TRIM

type     : CLASSREF { result = Factory.QREF(val[0][:value])  ; loc result, val[0] }

regex
  : REGEX { result = Factory.literal(val[0][:value]); loc result, val[0] }

#---MARKERS, SPECIAL TOKENS, SYNTACTIC SUGAR, etc.

endcomma
  : #
  | COMMA { result = nil }

endsemi
  : #
  | SEMIC

keyword
  : AND
  | CASE
  | CLASS
  | DEFAULT
  | DEFINE
  | ELSE
  | ELSIF
  | IF
  | IN
  | INHERITS
  | NODE
  | OR
  | UNDEF
  | UNLESS
  | TYPE
  | ATTR
  | FUNCTION
  | PRIVATE

nil
  : { result = nil}

end

---- header ----
require 'puppet'
require 'puppet/pops'

module Puppet
  class ParseError < Puppet::Error; end
  class ImportError < Racc::ParseError; end
  class AlreadyImportedError < ImportError; end
end

---- inner ----

# Make emacs happy
# Local Variables:
# mode: ruby
# End:
